package test;

import java.io.*;
import matrix.*;

public class Benchmark {
	
	public static Matrix fileToMatrix(String fileName) throws Exception {
		File f = new File(fileName);
		BufferedReader reader = new BufferedReader(new FileReader(f));
		int h = Integer.parseInt(reader.readLine());
		String s = "";
		String mat[][] = new String[h][];
		int i = 0;
		while ((s = reader.readLine()) != null)
			mat[i++] = s.split(" ");
		Matrix m = new Matrix(mat, Utils.TYPE_FLOAT);
		return m;
	}
	
	public static void main(String[] args) throws Exception {
		Matrix a = fileToMatrix("a.txt");
		Matrix b = fileToMatrix("b.txt");
		bench(new GaussElim(), a, b, false, Utils.TYPE_FLOAT, true);
		bench(new GaussElimPiv(), a, b, false, Utils.TYPE_FLOAT, true);
		bench(new LUDecomp(), a, b, false, Utils.TYPE_FLOAT, true);
		bench(new Jacobi(), a, b, false, Utils.TYPE_FLOAT, true);
		bench(new GaussSeidel(), a, b, false, Utils.TYPE_FLOAT, true);
	}
	
	public static void bench(SSLE ss, Matrix a, Matrix b, boolean buildSteps, int type, boolean stat) {
		System.out.println(ss.toString());
		if (ss instanceof Iterative)
			((Iterative) ss).set(new Matrix(new String[][] { { "1" }, { "1" }, { "1" } }, type), 10, 0);
		try {
			ss.solve(a, b, buildSteps);
			if (!stat)
				System.out.println(ss.soln());
			System.out.println(ss.absErr() + "\t" + ss.nSteps() + "\t" + ss.time() / 1000000.0 + "ms");
		} catch (Exception e) {
			System.out.println("Can't solve");
		}
	}
}